﻿namespace UnityEditor
{
    using System;
    using UnityEngine;

    [CustomEditor(typeof(SkinnedCloth))]
    internal class ClothInspector : Editor
    {
        private const int clothToolID = 0x4b0;
        private bool m_DidSelect;
        private int m_DrawMode = 1;
        private Vector3[] m_LastVertices;
        private int m_MeshVerticesPerSelectionVertex;
        private int m_MouseOver = -1;
        private float m_PaintCollisionSphereDistance;
        private bool m_PaintCollisionSphereDistanceEnabled;
        private float m_PaintCollisionSphereRadius = 0.5f;
        private bool m_PaintCollisionSphereRadiusEnabled;
        private float m_PaintMaxDistance = 0.2f;
        private float m_PaintMaxDistanceBias;
        private bool m_PaintMaxDistanceBiasEnabled;
        private bool m_PaintMaxDistanceEnabled;
        private bool m_RectSelecting;
        private bool[] m_RectSelection;
        private RectSelectionMode m_RectSelectionMode = RectSelectionMode.Add;
        private bool[] m_Selection;
        private Mesh m_SelectionMesh;
        private Vector2 m_SelectMousePoint;
        private Vector2 m_SelectStartPoint;
        private Mesh m_VertexMesh;
        private static int maxVertices;
        private static GUIContent s_PaintIcon = EditorGUIUtility.IconContent("ClothInspector.PaintValue", "Change this vertex coefficient value by painting in the scene view.");
        private static Material s_SelectionMaterial = null;
        private static GUIContent[] s_ToolIcons = new GUIContent[] { EditorGUIUtility.IconContent("ClothInspector.SelectTool", "Select vertices and edit their cloth coefficients in the inspector."), EditorGUIUtility.IconContent("ClothInspector.PaintTool", "Paint cloth coefficients on to vertices."), EditorGUIUtility.IconContent("ClothInspector.SettingsTool", "Set cloth options.") };
        private static ToolMode s_ToolMode = ToolMode.Settings;
        private static GUIContent s_ViewIcon = EditorGUIUtility.IconContent("ClothInspector.ViewValue", "Visualize this vertex coefficient value in the scene view.");

        private unsafe void ApplyRectSelection()
        {
            SkinnedCloth target = (SkinnedCloth) base.target;
            ClothSkinningCoefficient[] coefficients = target.coefficients;
            for (int i = 0; i < coefficients.Length; i++)
            {
                switch (this.m_RectSelectionMode)
                {
                    case RectSelectionMode.Replace:
                        this.m_Selection[i] = this.m_RectSelection[i];
                        break;

                    case RectSelectionMode.Add:
                        *((sbyte*) &(this.m_Selection[i])) |= this.m_RectSelection[i];
                        break;

                    case RectSelectionMode.Substract:
                        this.m_Selection[i] = this.m_Selection[i] && !this.m_RectSelection[i];
                        break;
                }
            }
        }

        private float CoefficientField(string label, float value, bool enabled, int mode)
        {
            bool flag = GUI.enabled;
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.ExpandWidth(false) };
            if (GUILayout.Toggle(this.m_DrawMode == mode, s_ViewIcon, "MiniButton", options))
            {
                this.m_DrawMode = mode;
                this.SetupMeshColors();
            }
            GUI.enabled = enabled;
            float num = EditorGUILayout.FloatField(label, value, new GUILayoutOption[0]);
            GUI.enabled = flag;
            GUILayout.EndHorizontal();
            return num;
        }

        private void DrawVertices()
        {
            if (this.SelectionMeshDirty())
            {
                this.GenerateSelectionMesh();
            }
            for (int i = 0; i < s_SelectionMaterial.passCount; i++)
            {
                s_SelectionMaterial.SetPass(i);
                Graphics.DrawMeshNow(this.m_SelectionMesh, Matrix4x4.identity);
            }
            if (this.m_MouseOver != -1)
            {
                Matrix4x4 matrix = Matrix4x4.TRS(this.m_LastVertices[this.m_MouseOver], Quaternion.identity, (Vector3) (0.02f * Vector3.one));
                s_SelectionMaterial.color = this.m_SelectionMesh.colors[this.m_MouseOver * this.m_MeshVerticesPerSelectionVertex];
                for (int j = 0; j < s_SelectionMaterial.passCount; j++)
                {
                    s_SelectionMaterial.SetPass(j);
                    Graphics.DrawMeshNow(this.m_VertexMesh, matrix);
                }
                s_SelectionMaterial.color = Color.white;
            }
        }

        private void GenerateSelectionMesh()
        {
            SkinnedCloth target = (SkinnedCloth) base.target;
            SkinnedMeshRenderer component = target.GetComponent<SkinnedMeshRenderer>();
            Vector3[] vertices = target.vertices;
            if (this.m_SelectionMesh != null)
            {
                UnityEngine.Object.DestroyImmediate(this.m_SelectionMesh);
            }
            this.m_SelectionMesh = new Mesh();
            this.m_SelectionMesh.hideFlags |= HideFlags.DontSave;
            CombineInstance[] combine = new CombineInstance[vertices.Length];
            this.m_MeshVerticesPerSelectionVertex = this.m_VertexMesh.vertices.Length;
            this.m_LastVertices = new Vector3[vertices.Length];
            Quaternion rotation = component.actualRootBone.rotation;
            Vector3 position = component.actualRootBone.position;
            for (int i = 0; (i < vertices.Length) && (i < maxVertices); i++)
            {
                this.m_LastVertices[i] = ((Vector3) (rotation * vertices[i])) + position;
                combine[i].mesh = this.m_VertexMesh;
                combine[i].transform = Matrix4x4.TRS(this.m_LastVertices[i], Quaternion.identity, (Vector3) (0.015f * Vector3.one));
            }
            this.m_SelectionMesh.CombineMeshes(combine);
            this.SetupMeshColors();
        }

        private float GetCoefficient(ClothSkinningCoefficient coefficient)
        {
            switch (this.m_DrawMode)
            {
                case 1:
                    return coefficient.maxDistance;

                case 2:
                    return coefficient.maxDistanceBias;

                case 3:
                    return coefficient.collisionSphereRadius;

                case 4:
                    return coefficient.collisionSphereDistance;
            }
            return 0f;
        }

        private int GetMouseVertex(Event e)
        {
            SkinnedCloth target = (SkinnedCloth) base.target;
            SkinnedMeshRenderer component = target.GetComponent<SkinnedMeshRenderer>();
            Vector3[] normals = target.normals;
            ClothSkinningCoefficient[] coefficients = target.coefficients;
            Ray ray = HandleUtility.GUIPointToWorldRay(e.mousePosition);
            float num = 1000f;
            int num2 = -1;
            Quaternion rotation = component.actualRootBone.rotation;
            bool flag = false;
            if ((SceneView.lastActiveSceneView != null) && (SceneView.lastActiveSceneView.renderMode == DrawCameraMode.Wireframe))
            {
                flag = true;
            }
            for (int i = 0; i < coefficients.Length; i++)
            {
                Vector3 lhs = this.m_LastVertices[i] - ray.origin;
                float sqrMagnitude = Vector3.Cross(lhs, ray.direction).sqrMagnitude;
                if (((Vector3.Dot((Vector3) (rotation * normals[i]), Camera.current.transform.forward) <= 0f) || flag) && ((sqrMagnitude < num) && (sqrMagnitude < 0.0025f)))
                {
                    num = sqrMagnitude;
                    num2 = i;
                }
            }
            return num2;
        }

        private void OnDisable()
        {
            UnityEngine.Object.DestroyImmediate(this.m_SelectionMesh);
        }

        private void OnEnable()
        {
            if (s_SelectionMaterial == null)
            {
                s_SelectionMaterial = EditorGUIUtility.LoadRequired("SceneView/VertexSelectionMaterial.mat") as Material;
            }
            SkinnedCloth target = (SkinnedCloth) base.target;
            ClothSkinningCoefficient[] coefficients = target.coefficients;
            this.m_Selection = new bool[coefficients.Length];
            this.m_RectSelection = new bool[coefficients.Length];
            this.m_VertexMesh = (Mesh) Resources.GetBuiltinResource(typeof(Mesh), "Cube.fbx");
            maxVertices = 0x10000 / this.m_VertexMesh.vertices.Length;
            if (target.vertices.Length >= maxVertices)
            {
                Debug.LogWarning("The mesh has too many vertices to be able to edit all skin coefficients. Only the first " + maxVertices + " vertices will be displayed");
            }
            this.GenerateSelectionMesh();
        }

        public override void OnInspectorGUI()
        {
            ToolMode mode = s_ToolMode;
            if (Tools.current != Tool.None)
            {
                s_ToolMode = ToolMode.Settings;
            }
            s_ToolMode = (ToolMode) GUILayout.Toolbar((int) s_ToolMode, s_ToolIcons, new GUILayoutOption[0]);
            if (s_ToolMode != mode)
            {
                GUIUtility.keyboardControl = 0;
                if (s_ToolMode != ToolMode.Settings)
                {
                    Tools.current = Tool.None;
                }
                SceneView.RepaintAll();
                this.SetupMeshColors();
            }
            switch (s_ToolMode)
            {
                case ToolMode.Select:
                    this.SelectionGUI();
                    break;

                case ToolMode.Paint:
                    this.PaintGUI();
                    break;

                case ToolMode.Settings:
                    base.DrawDefaultInspector();
                    break;
            }
        }

        public void OnPreSceneGUI()
        {
            if (s_ToolMode != ToolMode.Settings)
            {
                Handles.BeginGUI();
                int controlID = GUIUtility.GetControlID(FocusType.Passive);
                Event current = Event.current;
                EventType typeForControl = current.GetTypeForControl(controlID);
                if (typeForControl == EventType.MouseMove)
                {
                    int mouseOver = this.m_MouseOver;
                    this.m_MouseOver = this.GetMouseVertex(current);
                    if (this.m_MouseOver != mouseOver)
                    {
                        SceneView.RepaintAll();
                    }
                }
                else if (typeForControl == EventType.Layout)
                {
                    HandleUtility.AddDefaultControl(controlID);
                }
                switch (s_ToolMode)
                {
                    case ToolMode.Select:
                        this.SelectionPreSceneGUI(controlID);
                        break;

                    case ToolMode.Paint:
                        this.PaintPreSceneGUI(controlID);
                        break;
                }
                Handles.EndGUI();
            }
        }

        public void OnSceneGUI()
        {
            if (s_ToolMode != ToolMode.Settings)
            {
                if (Event.current.type == EventType.Repaint)
                {
                    this.DrawVertices();
                }
                Handles.BeginGUI();
                if ((this.m_RectSelecting && (s_ToolMode == ToolMode.Select)) && (Event.current.type == EventType.Repaint))
                {
                    EditorStyles.selectionRect.Draw(EditorGUIExt.FromToRect(this.m_SelectStartPoint, this.m_SelectMousePoint), GUIContent.none, false, false, false, false);
                }
                Handles.EndGUI();
            }
        }

        private float PaintField(string label, float value, ref bool enabled, int mode)
        {
            bool flag = GUI.enabled;
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.ExpandWidth(false) };
            if (GUILayout.Toggle(this.m_DrawMode == mode, s_ViewIcon, "MiniButton", options))
            {
                this.m_DrawMode = mode;
                this.SetupMeshColors();
            }
            GUILayoutOption[] optionArray2 = new GUILayoutOption[] { GUILayout.ExpandWidth(false) };
            enabled = GUILayout.Toggle(enabled, s_PaintIcon, "MiniButton", optionArray2);
            GUI.enabled = enabled;
            float num = EditorGUILayout.FloatField(label, value, new GUILayoutOption[0]);
            GUI.enabled = flag;
            GUILayout.EndHorizontal();
            return num;
        }

        private void PaintGUI()
        {
            this.m_PaintMaxDistance = this.PaintField("max Distance", this.m_PaintMaxDistance, ref this.m_PaintMaxDistanceEnabled, 1);
            this.m_PaintMaxDistanceBias = this.PaintField("distance bias", this.m_PaintMaxDistanceBias, ref this.m_PaintMaxDistanceBiasEnabled, 2);
            this.m_PaintMaxDistanceBias = Mathf.Clamp(this.m_PaintMaxDistanceBias, -1f, 1f);
            this.m_PaintCollisionSphereRadius = this.PaintField("collsionSphereRadius", this.m_PaintCollisionSphereRadius, ref this.m_PaintCollisionSphereRadiusEnabled, 3);
            this.m_PaintCollisionSphereDistance = this.PaintField("collisionSphereDistance", this.m_PaintCollisionSphereDistance, ref this.m_PaintCollisionSphereDistanceEnabled, 4);
        }

        private void PaintPreSceneGUI(int id)
        {
            Event current = Event.current;
            EventType typeForControl = current.GetTypeForControl(id);
            switch (typeForControl)
            {
                case EventType.MouseDown:
                case EventType.MouseDrag:
                {
                    SkinnedCloth target = (SkinnedCloth) base.target;
                    ClothSkinningCoefficient[] coefficients = target.coefficients;
                    if ((GUIUtility.hotControl != id) && ((current.alt || current.control) || (current.command || (current.button != 0))))
                    {
                        return;
                    }
                    if (typeForControl == EventType.MouseDown)
                    {
                        GUIUtility.hotControl = id;
                    }
                    int mouseVertex = this.GetMouseVertex(current);
                    if (mouseVertex != -1)
                    {
                        bool flag = false;
                        if (this.m_PaintMaxDistanceEnabled && (coefficients[mouseVertex].maxDistance != this.m_PaintMaxDistance))
                        {
                            coefficients[mouseVertex].maxDistance = this.m_PaintMaxDistance;
                            flag = true;
                        }
                        if (this.m_PaintMaxDistanceBiasEnabled && (coefficients[mouseVertex].maxDistanceBias != this.m_PaintMaxDistanceBias))
                        {
                            coefficients[mouseVertex].maxDistanceBias = this.m_PaintMaxDistanceBias;
                            flag = true;
                        }
                        if (this.m_PaintCollisionSphereRadiusEnabled && (coefficients[mouseVertex].collisionSphereRadius != this.m_PaintCollisionSphereRadius))
                        {
                            coefficients[mouseVertex].collisionSphereRadius = this.m_PaintCollisionSphereRadius;
                            flag = true;
                        }
                        if (this.m_PaintCollisionSphereDistanceEnabled && (coefficients[mouseVertex].collisionSphereDistance != this.m_PaintCollisionSphereDistance))
                        {
                            coefficients[mouseVertex].collisionSphereDistance = this.m_PaintCollisionSphereDistance;
                            flag = true;
                        }
                        if (flag)
                        {
                            target.coefficients = coefficients;
                            this.SetupMeshColors();
                            base.Repaint();
                        }
                    }
                    current.Use();
                    break;
                }
                default:
                    if (((typeForControl == EventType.MouseUp) && (GUIUtility.hotControl == id)) && (current.button == 0))
                    {
                        GUIUtility.hotControl = 0;
                        current.Use();
                    }
                    break;
            }
        }

        private bool RectSelectionModeFromEvent()
        {
            Event current = Event.current;
            RectSelectionMode replace = RectSelectionMode.Replace;
            if (current.shift)
            {
                replace = RectSelectionMode.Add;
            }
            if (current.alt)
            {
                replace = RectSelectionMode.Substract;
            }
            if (this.m_RectSelectionMode != replace)
            {
                this.m_RectSelectionMode = replace;
                return true;
            }
            return false;
        }

        private void SelectionGUI()
        {
            SkinnedCloth target = (SkinnedCloth) base.target;
            Vector3[] vertices = target.vertices;
            ClothSkinningCoefficient[] coefficients = target.coefficients;
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            if (GUILayout.Button("Select All", new GUILayoutOption[0]))
            {
                for (int j = 0; j < vertices.Length; j++)
                {
                    this.m_Selection[j] = true;
                }
                this.SetupMeshColors();
                SceneView.RepaintAll();
            }
            if (GUILayout.Button("Select None", new GUILayoutOption[0]))
            {
                for (int k = 0; k < vertices.Length; k++)
                {
                    this.m_Selection[k] = false;
                }
                this.SetupMeshColors();
                SceneView.RepaintAll();
            }
            float num3 = 0f;
            float num4 = 0f;
            float num5 = 0f;
            float num6 = 0f;
            int num7 = 0;
            for (int i = 0; i < coefficients.Length; i++)
            {
                if (this.m_Selection[i])
                {
                    num3 += coefficients[i].maxDistance;
                    num4 += coefficients[i].maxDistanceBias;
                    num5 += coefficients[i].collisionSphereRadius;
                    num6 += coefficients[i].collisionSphereDistance;
                    num7++;
                }
            }
            GUILayout.Label(num7 + " selected", new GUILayoutOption[0]);
            GUILayout.EndHorizontal();
            GUILayout.Space(5f);
            if (num7 > 0)
            {
                num3 /= (float) num7;
                num4 /= (float) num7;
                num5 /= (float) num7;
                num6 /= (float) num7;
            }
            float num9 = this.CoefficientField("max Distance", num3, num7 > 0, 1);
            float num10 = this.CoefficientField("distance bias", num4, num7 > 0, 2);
            float num11 = this.CoefficientField("collsionSphereRadius", num5, num7 > 0, 3);
            float num12 = this.CoefficientField("collisionSphereDistance", num6, num7 > 0, 4);
            num10 = Mathf.Clamp(num10, -1f, 1f);
            if (num9 != num3)
            {
                for (int m = 0; m < coefficients.Length; m++)
                {
                    if (this.m_Selection[m])
                    {
                        coefficients[m].maxDistance = num9;
                    }
                }
                target.coefficients = coefficients;
                this.SetupMeshColors();
            }
            if (num10 != num4)
            {
                for (int n = 0; n < coefficients.Length; n++)
                {
                    if (this.m_Selection[n])
                    {
                        coefficients[n].maxDistanceBias = num10;
                    }
                }
                target.coefficients = coefficients;
                this.SetupMeshColors();
            }
            if (num11 != num5)
            {
                for (int num15 = 0; num15 < coefficients.Length; num15++)
                {
                    if (this.m_Selection[num15])
                    {
                        coefficients[num15].collisionSphereRadius = num11;
                    }
                }
                target.coefficients = coefficients;
                this.SetupMeshColors();
            }
            if (num12 != num6)
            {
                for (int num16 = 0; num16 < coefficients.Length; num16++)
                {
                    if (this.m_Selection[num16])
                    {
                        coefficients[num16].collisionSphereDistance = num12;
                    }
                }
                target.coefficients = coefficients;
                this.SetupMeshColors();
            }
        }

        private bool SelectionMeshDirty()
        {
            SkinnedCloth target = (SkinnedCloth) base.target;
            SkinnedMeshRenderer component = target.GetComponent<SkinnedMeshRenderer>();
            Vector3[] vertices = target.vertices;
            Quaternion rotation = component.actualRootBone.rotation;
            Vector3 position = component.actualRootBone.position;
            for (int i = 0; i < this.m_LastVertices.Length; i++)
            {
                if (this.m_LastVertices[i] != ((rotation * vertices[i]) + position))
                {
                    return true;
                }
            }
            return false;
        }

        private void SelectionPreSceneGUI(int id)
        {
            Event current = Event.current;
            switch (current.GetTypeForControl(id))
            {
                case EventType.MouseDown:
                    if (((!current.alt && !current.control) && !current.command) && (current.button == 0))
                    {
                        GUIUtility.hotControl = id;
                        int mouseVertex = this.GetMouseVertex(current);
                        if (mouseVertex != -1)
                        {
                            if (current.shift)
                            {
                                this.m_Selection[mouseVertex] = !this.m_Selection[mouseVertex];
                            }
                            else
                            {
                                for (int i = 0; i < this.m_Selection.Length; i++)
                                {
                                    this.m_Selection[i] = false;
                                }
                                this.m_Selection[mouseVertex] = true;
                            }
                            this.m_DidSelect = true;
                            this.SetupMeshColors();
                            base.Repaint();
                        }
                        else
                        {
                            this.m_DidSelect = false;
                        }
                        this.m_SelectStartPoint = current.mousePosition;
                        current.Use();
                        return;
                    }
                    return;

                case EventType.MouseUp:
                    if ((GUIUtility.hotControl == id) && (current.button == 0))
                    {
                        GUIUtility.hotControl = 0;
                        if (!this.m_RectSelecting)
                        {
                            if ((!this.m_DidSelect && !current.alt) && (!current.control && !current.command))
                            {
                                SkinnedCloth target = (SkinnedCloth) base.target;
                                ClothSkinningCoefficient[] coefficients = target.coefficients;
                                for (int j = 0; j < coefficients.Length; j++)
                                {
                                    this.m_Selection[j] = false;
                                }
                            }
                        }
                        else
                        {
                            EditorApplication.modifierKeysChanged = (EditorApplication.CallbackFunction) Delegate.Remove(EditorApplication.modifierKeysChanged, new EditorApplication.CallbackFunction(this.SendCommandsOnModifierKeys));
                            this.m_RectSelecting = false;
                            this.RectSelectionModeFromEvent();
                            this.ApplyRectSelection();
                        }
                        this.SetupMeshColors();
                        base.Repaint();
                    }
                    return;

                case EventType.MouseDrag:
                    if (GUIUtility.hotControl == id)
                    {
                        if (!this.m_RectSelecting)
                        {
                            Vector2 vector = current.mousePosition - this.m_SelectStartPoint;
                            if (((vector.magnitude > 2f) && !current.alt) && (!current.control && !current.command))
                            {
                                EditorApplication.modifierKeysChanged = (EditorApplication.CallbackFunction) Delegate.Combine(EditorApplication.modifierKeysChanged, new EditorApplication.CallbackFunction(this.SendCommandsOnModifierKeys));
                                this.m_RectSelecting = true;
                                this.RectSelectionModeFromEvent();
                                this.SetupMeshColors();
                            }
                        }
                        if (this.m_RectSelecting)
                        {
                            this.m_SelectMousePoint = new Vector2(Mathf.Max(current.mousePosition.x, 0f), Mathf.Max(current.mousePosition.y, 0f));
                            if (this.RectSelectionModeFromEvent() || this.UpdateRectSelection())
                            {
                                this.SetupMeshColors();
                            }
                            current.Use();
                        }
                    }
                    return;

                case EventType.ExecuteCommand:
                    if ((this.m_RectSelecting && (current.commandName == "ModifierKeysChanged")) && (this.RectSelectionModeFromEvent() || this.UpdateRectSelection()))
                    {
                        this.SetupMeshColors();
                    }
                    return;
            }
        }

        internal void SendCommandsOnModifierKeys()
        {
            SceneView.lastActiveSceneView.SendEvent(EditorGUIUtility.CommandEvent("ModifierKeysChanged"));
        }

        private void SetupMeshColors()
        {
            SkinnedCloth target = (SkinnedCloth) base.target;
            ClothSkinningCoefficient[] coefficients = target.coefficients;
            Color[] colorArray = new Color[this.m_SelectionMesh.vertices.Length];
            float num = 0f;
            float num2 = 0f;
            for (int i = 0; i < coefficients.Length; i++)
            {
                float coefficient = this.GetCoefficient(coefficients[i]);
                if (coefficient < num)
                {
                    num = coefficient;
                }
                if (coefficient > num2)
                {
                    num2 = coefficient;
                }
            }
            for (int j = 0; (j < coefficients.Length) && (j < maxVertices); j++)
            {
                for (int k = 0; k < this.m_MeshVerticesPerSelectionVertex; k++)
                {
                    Color red;
                    bool flag = this.m_Selection[j];
                    if (this.m_RectSelecting)
                    {
                        switch (this.m_RectSelectionMode)
                        {
                            case RectSelectionMode.Replace:
                                flag = this.m_RectSelection[j];
                                break;

                            case RectSelectionMode.Add:
                                flag |= this.m_RectSelection[j];
                                break;

                            case RectSelectionMode.Substract:
                                flag = flag && !this.m_RectSelection[j];
                                break;
                        }
                    }
                    if (flag)
                    {
                        red = Color.red;
                    }
                    else
                    {
                        float num7;
                        if ((num2 - num) != 0f)
                        {
                            num7 = (this.GetCoefficient(coefficients[j]) - num) / (num2 - num);
                        }
                        else
                        {
                            num7 = 0.5f;
                        }
                        if (num7 < 0.5f)
                        {
                            red = Color.Lerp(Color.green, Color.yellow, 2f * num7);
                        }
                        else
                        {
                            red = Color.Lerp(Color.yellow, Color.blue, (2f * num7) - 1f);
                        }
                    }
                    colorArray[(j * this.m_MeshVerticesPerSelectionVertex) + k] = red;
                }
            }
            this.m_SelectionMesh.colors = colorArray;
        }

        private bool UpdateRectSelection()
        {
            bool flag = false;
            SkinnedCloth target = (SkinnedCloth) base.target;
            SkinnedMeshRenderer component = target.GetComponent<SkinnedMeshRenderer>();
            Vector3[] normals = target.normals;
            ClothSkinningCoefficient[] coefficients = target.coefficients;
            float x = Mathf.Min(this.m_SelectStartPoint.x, this.m_SelectMousePoint.x);
            float num2 = Mathf.Max(this.m_SelectStartPoint.x, this.m_SelectMousePoint.x);
            float y = Mathf.Min(this.m_SelectStartPoint.y, this.m_SelectMousePoint.y);
            float num4 = Mathf.Max(this.m_SelectStartPoint.y, this.m_SelectMousePoint.y);
            Ray ray = HandleUtility.GUIPointToWorldRay(new Vector2(x, y));
            Ray ray2 = HandleUtility.GUIPointToWorldRay(new Vector2(num2, y));
            Ray ray3 = HandleUtility.GUIPointToWorldRay(new Vector2(x, num4));
            Ray ray4 = HandleUtility.GUIPointToWorldRay(new Vector2(num2, num4));
            Plane plane = new Plane(ray2.origin + ray2.direction, ray.origin + ray.direction, ray.origin);
            Plane plane2 = new Plane(ray3.origin + ray3.direction, ray4.origin + ray4.direction, ray4.origin);
            Plane plane3 = new Plane(ray.origin + ray.direction, ray3.origin + ray3.direction, ray3.origin);
            Plane plane4 = new Plane(ray4.origin + ray4.direction, ray2.origin + ray2.direction, ray2.origin);
            Quaternion rotation = component.actualRootBone.rotation;
            bool flag2 = false;
            if ((SceneView.lastActiveSceneView != null) && (SceneView.lastActiveSceneView.renderMode == DrawCameraMode.Wireframe))
            {
                flag2 = true;
            }
            for (int i = 0; i < coefficients.Length; i++)
            {
                Vector3 inPt = this.m_LastVertices[i];
                bool flag3 = Vector3.Dot((Vector3) (rotation * normals[i]), Camera.current.transform.forward) <= 0f;
                bool flag4 = (((plane.GetSide(inPt) && plane2.GetSide(inPt)) && plane3.GetSide(inPt)) && plane4.GetSide(inPt)) && (flag2 || flag3);
                if (this.m_RectSelection[i] != flag4)
                {
                    this.m_RectSelection[i] = flag4;
                    flag = true;
                }
            }
            return flag;
        }

        private enum RectSelectionMode
        {
            Replace,
            Add,
            Substract
        }

        private enum ToolMode
        {
            Select,
            Paint,
            Settings
        }
    }
}

